﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ConsoleApplication1;

namespace PresentForms
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        Present present;
        List<AbstractSweet> mainPresent;
        List<AbstractSweet> sweetList;

        IPrinter printer, printerBox;
            

        private void button2_Click(object sender, EventArgs e)
        {
            SweetCalculator calc = new SweetCalculator();
            WeighTextBox.Text = calc.GetTotalWeight(present).ToString();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            printer = SweetPrinter.GetPrinterFromTypes("file");
            printerBox = SweetPrinter.GetPrinterFromTypes("textBox");

            mainPresent = new List<AbstractSweet>();
            present = new Present();

            sweetList = new List<AbstractSweet>();

            mainPresent = Create();
            string listLine = String.Empty;
            int count = 1;
            foreach (AbstractSweet pr in mainPresent)
            {
                listLine = count + ")   " + pr.Name + " " + pr.Weight + Environment.NewLine;
                SweetListBox.Items.Add(listLine.ToString());
                count++;
            }
            
            
        }

        static List<AbstractSweet> Create()
        {
            ChocolateWithWaffle burevestnik = new ChocolateWithWaffle();
            burevestnik.Name = "Буревестник";
            burevestnik.Weight = 20;

            CaramelInChololate yashkino = new CaramelInChololate();
            yashkino.Name = "Яшкино";
            yashkino.Weight = 45;

            ChocolateSweet metelitsa = new ChocolateSweet();
            metelitsa.SetKind("белый");
            metelitsa.Name = "Метелица";
            metelitsa.Weight = 59;

            ChocolateSweet korkunov = new ChocolateSweet();
            korkunov.SetKind("черный");
            korkunov.Name = "Коркунов";
            korkunov.Weight = 13.5;

            ChocolateSweet roshen = new ChocolateSweet();
            roshen.SetKind("молочный");
            roshen.Name = "Рошен";
            roshen.Weight = 24;

            CaramelSweet strawberry = new CaramelSweet();
            strawberry.SetTaste("клубника");
            strawberry.Name = "Клубничка";
            strawberry.Weight = 68.95;

            CaramelSweet orange = new CaramelSweet();
            orange.SetTaste("апельсин");
            orange.Name = "Апельсинка";
            orange.Weight = 57.15;

            CaramelSweet vanilla = new CaramelSweet();
            vanilla.SetTaste("ваниль");
            vanilla.Name = "Ванилька";
            vanilla.Weight = 87.85;

            List<AbstractSweet> presents = new List<AbstractSweet>();
            presents.Add(metelitsa);
            presents.Add(korkunov);
            presents.Add(roshen);

            presents.Add(strawberry);
            presents.Add(orange);
            presents.Add(vanilla);

            return presents;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int index = SweetListBox.SelectedIndex; // Получаем номер выделенной строки
            
            Creator[] creators = new Creator[4];
            creators[0] = new CaramelCreator();
            creators[1] = new CaramelInChololateCreator();
            creators[2] = new ChocolateCreator();
            creators[3] = new ChocolateWithWaffleCreator();

            if (mainPresent[index] is CaramelSweet)
            {
                sweetList.Add(creators[0].FactoryMethod(mainPresent[index].Name, mainPresent[index].Weight));
            }
            else if (mainPresent[index] is CaramelInChololate)
            {
                sweetList.Add(creators[1].FactoryMethod(mainPresent[index].Name, mainPresent[index].Weight));
            }
            else if (mainPresent[index] is ChocolateSweet)
            {
                sweetList.Add(creators[2].FactoryMethod(mainPresent[index].Name, mainPresent[index].Weight));
            }
            else if (mainPresent[index] is ChocolateWithWaffle)
            {
                sweetList.Add(creators[3].FactoryMethod(mainPresent[index].Name, mainPresent[index].Weight));
            }

            present.Clear();
            foreach (AbstractSweet pr in sweetList)
            {
                present.Add(pr);
            }

            printer.Print(present, SweetListBox);
            printerBox.Print(present, PresentListBox);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                int index = PresentListBox.SelectedIndex; // Получаем номер выделенной строки
                sweetList.RemoveAt(index);

                present.Clear();
                foreach (AbstractSweet pr in sweetList)
                {
                    present.Add(pr);
                }
                printerBox.Print(present, PresentListBox);
            }
            catch
            {
                MessageBox.Show("Не выбрана ни одна конфета из подарка");
            }
        }
    }
}
